function heaer(){
    var winH = $('.wrapper').height();
    var herH = $(".navbar-fixed-top").height();
    $(".wrapper-content").height(winH-herH);
    $(window).resize(function () {
        winH = $('.wrapper').height();
        herH = $(".navbar-fixed-top").height();
        $(".wrapper-content").height(winH-herH);
    });
}
function menuEvent(){
    $('a[src-data]').on('click',function () {
        var topW = $(".navbar-fixed-top", parent.document).width();
        var maxnavbarW =topW-$(".navbar-fixed-top ul:first", parent.document).width()-$(".navbar-fixed-top ul:last", parent.document).width()-54;
        var navbarW=$(".navbar-tabs", parent.document).width()+300;
        var src = $(this).attr("src-data");
        var text = $(this).attr("src-title");
        if($("#rightMain iframe",parent.document).is("[src='"+src+"']")){
            $("#rightMain iframe[src='"+src+"']",parent.document).show().siblings().hide();
            $(".navbar-tabs li a[data='"+src+"']",parent.document).parent().addClass("active").siblings().removeClass("active");
        }else {
            var html = '';
            html+='<iframe src="'+src+'" width="100%" height="100%" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling-x="no" scrolling-y="auto" allowtransparency="yes"></iframe>';
            $("#rightMain", parent.document).append(html);
            $("#rightMain iframe[src='"+src+"']", parent.document).siblings().hide();
            if(navbarW<maxnavbarW){
                var htmla='';
                htmla+='<li class="active"><a data="'+src+'">'+text+'</a><button type="button" class="close"><span>&times;</span></button></li>';
                $(".navbar-tabs li", parent.document).removeClass("active");
                $(".navbar-tabs", parent.document).append(htmla);
            }else if(navbarW>maxnavbarW){
                var navlist = '';
                navlist+='<li style="position: relative"><a data="'+src+'" >'+text+'</a><button type="button" class="close" style="position: absolute;top:3px;right: 5px;"><span>&times;</span></button></li>';
                $(".tabs-list ul", parent.document).append(navlist);
                $(".tabs-list", parent.document).removeClass("hide");
            }
        }
    });
}
function openmenu(){
    $('#leftnav').on('click',"li>a",function () {
        var $liactive = $(this).closest('li');
        if($liactive.hasClass('active')){
            $liactive.find("ul").slideUp("normal",function(){
                $liactive.removeClass('active');
            });
        }else{
            $('#leftnav>li>ul>li').removeClass('active');
            $liactive.find("ul").slideDown("normal");
            $liactive.addClass("active").siblings(".active").removeClass("active");
            $liactive.siblings("li").find("ul").slideUp("normal");
        }
    });
}
function showHideBar(){
    var small = function(){
        $(".navbar-static-side").animate({width:"70px"}, "normal").addClass("navbar-small");
        $(".wrapper-page").animate({marginLeft:"70px"}, "normal");
        $(".nav-side-green").text("云平台");
        $(".nav-second-level").css("display","");
    }
    var big = function(){
        $(".navbar-static-side").animate({width:"220px"}, "normal",function () {
            $(".nav-side-green").text("云安全控制台");
            $(".navbar-static-side").removeClass("navbar-small");
            $("li.active .nav-second-level").css("display","block");
        });
        $(".wrapper-page").animate({marginLeft:"220px"}, "normal");
    }
    if($(".wrapper").width()<=768){
        small()
    }else if($(".wrapper").width()>768){
        $('#BarBtn').on('click',function () {
            var sideW = $(".navbar-static-side").width();
            if(sideW == 220){
                small();
            }else {
                big();
            }
        });
    }
}
function navbarTabs(){
    $(".navbar-tabs").on("click","li>a",function () {
        var src = $(this).attr("data");
        $("#rightMain iframe[src='"+src+"']").show().siblings().hide();
        $(this).closest("li").addClass("active").siblings().removeClass("active");
        $("#leftnav >li").removeClass("active");
        $("#leftnav li ul").slideUp("normal");
    });
    $(".navbar-tabs").on("click","li > button",function(){
        var src = $(this).prev("a").attr("data");
        var li = $(this).closest("li");
        var thisli = li.index();
        var lilength = $(".navbar-tabs li").length;
        $("#leftnav >li").removeClass("active");
        $("#leftnav li ul").slideUp("normal");
        if(li.hasClass('active')){
            if(thisli+1 == lilength){
                li.prev('li').addClass("active");
                $("#rightMain iframe[src='"+src+"']").prev('iframe').show();
            }else {
                li.next('li').addClass("active");
                $("#rightMain iframe[src='"+src+"']").next('iframe').show();
            }
        }
        console.log(lilength);
        console.log(1);
        if(lilength=1){
            $(".home").show();
        }
        li.remove();
        $("#rightMain iframe[src='"+src+"']").remove();
    });
    $(".tabs-list").on("click","li>a",function () {
        var src = $(this).attr("data");
        $("#rightMain iframe[src='"+src+"']").show().siblings().hide();
        $(this).closest("li").addClass("active").siblings().removeClass("active");
        $("#leftnav >li").removeClass("active");
        $("#leftnav li ul").slideUp("normal");
        $(".navbar-tabs li").removeClass("active");
    });
    $(".tabs-list").on("click","li > button",function(){
        var src = $(this).prev("a").attr("data");
        var li = $(this).closest("li");
        var thisli = li.index();
        var lilength = $(".tabs-list li").length;
        $("#leftnav >li").removeClass("active");
        $("#leftnav li ul").slideUp("normal");
        if(thisli+2 == lilength){
            $("#rightMain iframe[src='"+src+"']").prev('iframe').show();
            $(".navbar-tabs li:last").addClass("active");
        }else {
            $("#rightMain iframe[src='"+src+"']").next('iframe').show();
        }
        if(lilength==2){
            $(".tabs-list").addClass("hide");
        }
        li.remove();
        $("#rightMain iframe[src='"+src+"']").remove();
    });
}
function closeBtn(){
    $("button.close").on("click",function () {
        $(this).parent().remove();
    })
}
function collapse(){
    $(".collapse-link").on("click",function () {
        if($(this).find("i").hasClass("fa-chevron-up")){
            $(this).closest(".ibox-title").next(".ibox-content").slideUp();
            $(this).find(".fa-chevron-up").removeClass("fa-chevron-up").addClass("fa-chevron-down");
        }else if($(this).find("i").hasClass("fa-chevron-down")){
            $(this).closest(".ibox-title").next(".ibox-content").slideDown();
            $(this).find(".fa-chevron-down").removeClass("fa-chevron-down").addClass("fa-chevron-up");
        }
    })
}
function dropdown(){
    $(".yun-dropdown-trigger").on("click",function () {
        var menu = $(this).next(".yun-dropdown-menu");
        var open = $(this).parent();
        if(open.hasClass("open")){
            menu.fadeOut("show");
            open.removeClass("open");
        }else {
            $(".yun-dropdown").removeClass("open");
            $(".yun-dropdown-menu").fadeOut("show");
            menu.fadeIn("show");
            open.addClass("open");
        }
        $(".confirm,.clear").on("click",function () {
            $(this).closest(".yun-dropdown").removeClass("open");
            $(this).closest(".yun-dropdown-menu").fadeOut("show");
        })
    });
    $(document).on("click",function(e){
        var target  = $(e.target);
        if(target.closest(".yun-dropdown-menu,.yun-dropdown-trigger").length == 0){
            $(".yun-dropdown-menu").fadeOut("show");
            $(".yun-dropdown").removeClass("open");
        };
        e.stopPropagation();
    })
}
function pageNav() {
    $(".page-nav .page-nav-tab").on("click",function () {
        var aria = $(this).attr("aria-controls");
        $(this).addClass("active").siblings().removeClass("active");
        $("#"+aria).show().siblings().hide();
    })
}

$(function () {
    heaer(); /*计算高度*/
    menuEvent();/*左侧菜单链接*//*修改企业信息、电话、密码链接*/
    openmenu();/*二级菜单显示*/
    showHideBar();/*显示隐藏左侧栏*/
    navbarTabs();/*顶部导航条*/
    closeBtn();/*关闭按钮*/
    collapse();/*折叠*/
    dropdown();/*弹出下拉框*/
    pageNav();/*页内导航切换*/
});